﻿/**
* tokenInterceptorService - AngularJS module for web api management.
*
*
* Credits
* =======
*
* Copyright 2016 LonghronShen <LonghronShen@Hotmail.com>
*/

(function () {
    "use strict";

    /**
     * Config
     */
    var moduleName = 'ccp';
    var DEFAULT_ID = '__default';

    /**
     * Module
     */
    var module;
    try {
        module = angular.module(moduleName);
    } catch (err) {
        // named module does not exist, so create one
        module = angular.module(moduleName, []);
    }

    module.service('tokenInterceptorService', ["$rootScope", '$window', function ($rootScope, $window) {
        return {
            request: function (config) {
                config.headers = config.headers || {};
                // console.info("Challenging token: ", $window.sessionStorage.token);
                if ($window.sessionStorage.token) {
                    config.headers.Token = $window.sessionStorage.token;
                }
                return config;
            },

            response: function (response) {
                return response || $q.when(response);
            }
        };
    }]);
})();